package com.zr.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.dao.TeacherDaoHyx;
import com.zr.model.Teacher;
import com.zr.util.JDBCUtil;

public class TeacherDaoImplHyx implements TeacherDaoHyx{

	/**
	 * 查找当前老师
	 */
	@Override
	public Teacher selectTeacherByTid(int tid) {
		Teacher t = new Teacher();
		StringBuffer sql = new StringBuffer("");
		sql.append(" select *");
		sql.append(" from teacher where teacher.tid=?");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, tid);		
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				t.setTid(rs.getInt("tid"));
				t.setTname(rs.getString("tname"));
				t.setTsex(rs.getString("tsex"));
				t.setTage(rs.getString("tage"));
				t.setAddress(rs.getString("address"));
				t.setTpnumber(rs.getString("tpnumber"));
			}
			
			JDBCUtil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return t;		
	}

	/**
	 * 修改当前老师的个人信息
	 */
	@Override
	public boolean updateTeacher(int tid, String tname, String tage, String tsex, String address, String tpnumber) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		sql.append(" UPDATE teacher SET teacher.tname=?,teacher.tage=?,teacher.tsex=?,");
		sql.append(" teacher.address=?,teacher.tpnumber=? where teacher.tid=?");
		Connection con = JDBCUtil.getConnection();
	 	try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, tname);
			pst.setString(2, tage);
			pst.setString(3, tsex);
			pst.setString(4, address);
			pst.setString(5, tpnumber);
			pst.setInt(6, tid);
			int i = pst.executeUpdate();
			if(i>0){
				flag=true;
			}
			
			JDBCUtil.closeJDBC(pst, con);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 	return flag;
	}

//根据姓名查找老师
	@Override
	public Teacher selectTeacherByTname(String tname) {
		Teacher t = new Teacher();
		StringBuffer sql = new StringBuffer("");
		sql.append(" select *");
		sql.append(" from teacher where teacher.tname=?");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, tname);		
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				t.setTid(rs.getInt("tid"));
				t.setTname(rs.getString("tname"));
				t.setTsex(rs.getString("tsex"));
				t.setTage(rs.getString("tage"));
				t.setAddress(rs.getString("address"));
				t.setTpnumber(rs.getString("tpnumber"));
			}
			
			JDBCUtil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return t;		
	}
//根据姓名判断老师是否存在
	@Override
	public int findTeacherByTname(String tname) {
		int i = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(teacher.tname) as cts FROM teacher");
		sql.append(" WHERE teacher.tname=?");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, tname);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
					i = rs.getInt("cts");
			}
			
			JDBCUtil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}
}
